/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mobilottery.UI.Forms;

import Mobilottery.Interfaces.IAlertCommandListener;
import Mobilottery.AppManager;
import Mobilottery.Constants.Consts;
import Mobilottery.Constants.Consts.Screens;
import Mobilottery.Interfaces.IBackgroundOperation;
import Mobilottery.Utils.UtilsConnection;
import Mobilottery.XmlManager;
import Mobilottery.XmlManager.MyGamesDetails;
import Mobilottery.XmlManager.OpenGamesDetails;
import javax.microedition.lcdui.*;

/**
 *
 * @author WindowsVista
 */
public class FormMainMenu extends Form implements CommandListener, ItemCommandListener
{

    final String MENU_TEXT_NEW_GAME = "New Game";
    final String MENU_TEXT_MY_GAMES = "My Games";
    final String MENU_TEXT_ABOUT = "About";

    public FormMainMenu(Object p_obj)
    {
        super("MainMenu");

        Command cmd;
        ImageItem img;

        img = new ImageItem(
                null,
                Consts.UIConsts.IMAGES_LOGO,
                Item.LAYOUT_VCENTER | Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER,
                "MOBILottery",
                Item.PLAIN);
        this.append(img);


        img = new ImageItem(
                null,
                Consts.UIConsts.IMAGES_MENU_NEW_GAME,
                Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER,
                MENU_TEXT_NEW_GAME,
                Item.PLAIN);
        cmd = new Command("Select", Command.OK, 0);
        img.setDefaultCommand(cmd);
        img.setItemCommandListener(this);
        this.append(img);

        img = new ImageItem(
                null,
                Consts.UIConsts.IMAGES_MENU_MY_GAMES,
                Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER,
                MENU_TEXT_MY_GAMES,
                Item.PLAIN);
        img.setDefaultCommand(cmd);
        img.setItemCommandListener(this);
        this.append(img);

        img = new ImageItem(
                null,
                Consts.UIConsts.IMAGES_MENU_ABOUT,
                Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER,
                MENU_TEXT_ABOUT,
                Item.PLAIN);
        img.setDefaultCommand(cmd);
        img.setItemCommandListener(this);
        this.append(img);

        cmd = new Command("Exit", Command.EXIT, 0);
        this.addCommand(cmd);

        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        int cmdType = c.getCommandType();

//        if (cmdType == Command.OK)
//        {
//            menu_Clicked(c.getPriority());
//        }
        //else
        if (cmdType == Command.EXIT)
        {
            exit_Clicked();
        }
    }

    private void exit_Clicked()
    {
        AppManager.setAlert(
                "Exit Mobilottery",
                "Are you sure you want to close application?",
                AlertType.CONFIRMATION,
                new IAlertCommandListener()
                {

                    public boolean onOK()
                    {
                        AppManager.closeApplication();
                        return false;
                    }

                    public boolean onCancel()
                    {
                        return true;
                    }
                });
    }

    public void commandAction(Command c, Item p_item)
    {
        System.out.println("MainMenuForm   |  commandAction ITEM " + c.getPriority() + "   | Start ");

        if (p_item instanceof ImageItem)
        {
            ImageItem imageItem = (ImageItem) p_item;

            int cmdType = c.getCommandType();
            if (cmdType == Command.OK)
            {
                String altText = imageItem.getAltText();
                if (altText.equals(MENU_TEXT_NEW_GAME) == true)
                {
                    menuNewGame_Clicked();
                }
                else if (altText.equals(MENU_TEXT_MY_GAMES) == true)
                {
                    menuMyGames_Clicked();
                }
                else if (altText.equals(MENU_TEXT_ABOUT) == true)
                {
                    menuAbout_Clicked();
                }
            }
        }
    }

    private void menuAbout_Clicked()
    {
        AppManager.goToScreen(Screens.About, null);
    }

    private void menuNewGame_Clicked()
    {
        AppManager.runBackgroundOperation(
                "New Game",
                "Downloading available games...",
                new IBackgroundOperation()
                {

                    public boolean execute()
                    {
                        // Create request xml
                        String requestUrl =
                                Consts.Urls.NewGame +
                                "&userid=" + AppManager.Username +
                                "&pass=" + AppManager.Password;
                        String recvXml = UtilsConnection.GetData(requestUrl);

                        // for testing...................
                        //recvXml = "<root><status>0</status><status_desc>error</status_desc></root>";
                        //recvXml = "<root><status>1</status><status_desc>OK</status_desc>" +
//                                  "<games>" +
//                                  "<g num='1' win_price='1001'>Lotto</g>" +
//                                  "<g num='2' win_price='1002'>Lotto</g>" +
//                                  "<g num='3' win_price='1003'>Lotto</g>" +
//                                  "<g num='4' win_price='1004'>Lotto</g>" +
//                                  "<g num='5' win_price='1005'>Lotto</g>" +
//                                  "<g num='6' win_price='1006'>Lotto</g>" +
//                                  "<g num='7' win_price='1007'>Lotto</g>" +
//                                  "</games>" +
//                                  "</root>";

                        OpenGamesDetails openGamesDetails = XmlManager.ProcessXml_OpenGames(recvXml);
                        if (openGamesDetails.dataCorrect() == false)
                        {
                            // data inccorect...
                            AppManager.setAlert(
                                    "New Game", openGamesDetails.StatusDesc, AlertType.INFO, "Continue", null,
                                    new IAlertCommandListener()
                                    {

                                        public boolean onOK()
                                        {
                                            // return to current screen
                                            return true;
                                        }

                                        public boolean onCancel()
                                        {
                                            // not getting here (not cancel button)
                                            return true;
                                        }
                                    });
                        }
                        else
                        {
                            // data correct!!

                            AppManager.goToScreen(Screens.NewGame1, openGamesDetails);
                        }


                        // manualy switching screen, so don't need to return to previos screen
                        return false;
                    }
                });
    }

    private void menuMyGames_Clicked()
    {
        AppManager.runBackgroundOperation(
                "My Games",
                "Downloading participated games lost...",
                new IBackgroundOperation()
                {

                    public boolean execute()
                    {
                        // Create request xml
                        String requestUrl =
                                Consts.Urls.MyGames +
                                "&userid=" + AppManager.Username +
                                "&pass=" + AppManager.Password;
                        String recvXml = UtilsConnection.GetData(requestUrl);

                        // for testing...................
//                        recvXml = "<root><status>0</status><status_desc>error</status_desc></root>";
//                        recvXml = "<root><status>1</status><status_desc>OK</status_desc>" +
//                                  "<games>" +
//                                  "<g id='1' win_price='200' date='26-01-2009' trns_id='AVB123111' participation_info='11 22 33 44 01'>Lotto</g>" +
//                                  "<g id='1' win_price='201' date='26-01-2009' trns_id='AVB123111' participation_info='11 22 33 44 01'>Lotto</g>" +
//                                  "<g id='1' win_price='1001' date='04-01-2009' win_results='10 22 33 44 55' trns_id='AVB123111' participation_info='11 22 33 44 01' user_win_amount='0'>Lotto</g>" +
//                                  "<g id='2' win_price='1002' date='03-01-2009' win_results='20 22 33 44 55' trns_id='AVB123222' participation_info='11 22 33 44 02' user_win_amount='99'>Lotto</g>" +
//                                  "<g id='3' win_price='1003' date='02-01-2009' win_results='30 22 33 44 55' trns_id='AVB123333' participation_info='11 22 33 44 03' user_win_amount='0'>Lotto</g>" +
//                                  "<g id='4' win_price='1004' date='01-01-2009' win_results='40 22 33 44 55' trns_id='AVB123444' participation_info='11 22 33 44 04' user_win_amount='5000'>Lotto</g>" +
//                                  "</games>" +
//                                  "</root>";

                        MyGamesDetails myGamesDetails = XmlManager.ProcessXml_MyGames(recvXml);
                        if (myGamesDetails.dataCorrect() == false)
                        {
                            // data inccorect...
                            AppManager.setAlert(
                                    "New Game", myGamesDetails.StatusDesc, AlertType.INFO, "Continue", null,
                                    new IAlertCommandListener()
                                    {

                                        public boolean onOK()
                                        {
                                            // return to current screen
                                            return true;
                                        }

                                        public boolean onCancel()
                                        {
                                            // not getting here (not cancel button)
                                            return true;
                                        }
                                    });
                        }
                        else
                        {
                            // data correct!!

                            AppManager.goToScreen(Screens.MyGames, myGamesDetails);
                        }


                        // manualy switching screen, so don't need to return to previos screen
                        return false;
                    }
                });

    }
}
